package job.manager.db.dao;

import java.util.Collection;
import job.manager.db.ec.TimeScheduleEB;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import job.manager.db.ec.TimeScheduleRowEB;

/**
 *  DAO class is responsible for communcation with the
 *  database regarding timeSechdules.
 *  A timeShecdule is a time period and has an start and end TimeReport
 * 
 */
public class TimeScheduleDAO {

    EntityManager em;

    public TimeScheduleDAO(EntityManager em) {
        this.em = em;
    }

    public TimeScheduleEB getTimeSchedule(Integer pk) {
        Query q = em.createNamedQuery("TimeScheduleEB.findById");
        q.setParameter("id", pk);
        return (TimeScheduleEB) q.getSingleResult();
    }

    public int update(TimeScheduleEB report) {
        if (report != null) {
            TimeScheduleEB r = em.merge(report);
            em.flush();
            if (em.contains(report)) {
                return 1;
            }
        }
        return 0;
    }

    public TimeScheduleEB insert(TimeScheduleEB report) {
        if (report != null) {
            em.persist(report);
            em.flush();
            if (em.contains(report)) {
                return report;
            }
        }
        return null;
    }

    public TimeScheduleRowEB insert(TimeScheduleRowEB row) {
        if (row != null) {
            em.persist(row);
            em.flush();
            if (em.contains(row)) {
                return row;
            }
        }
        return null;
    }

     

    public int delete(TimeScheduleEB report) {
        if (report != null) {
            TimeScheduleEB r = em.merge(report);

            em.remove(r);

            if (!em.contains(r)) {
                return 1;
            }
        }
        return 0;
    }

    public Collection<TimeScheduleEB> getAllTimeSchedule() {

        Query q = em.createNamedQuery("TimeScheduleEB.findAll");

        return q.getResultList();
    }

    public TimeScheduleRowEB getTimeScheduleRow(Integer pk)
    {
        Query q = em.createNamedQuery("TimeScheduleRowEB.findById");
        q.setParameter("id", pk);
        return (TimeScheduleRowEB) q.getSingleResult();
    }

    public int update(TimeScheduleRowEB row) {
        if (row != null) {
            TimeScheduleRowEB r = em.merge(row);
            em.flush();
            if (em.contains(row)) {
                return 1;
            }
        }
        return 0;
    }

    public int delete(TimeScheduleRowEB row) {
        if (row != null) {
            TimeScheduleRowEB r = em.merge(row);

            em.remove(r);

            if (!em.contains(r)) {
                return 1;
            }
        }
        return 0;
    }

    public Collection<TimeScheduleRowEB> getAllTimeScheduleRow() {

        Query q = em.createNamedQuery("TimeScheduleRowEB.findAll");

        return q.getResultList();
    }
}
